package jp.sourceforge.akjrcp.akj_betas;

import java.io.File;
import java.io.IOException;

import jp.sourceforge.akjrcp.audiotools.AudioToolsSettingPage;
import jp.sourceforge.akjrcp.dropsomething.FFMpegSettingPage;
import jp.sourceforge.akjrcp.dropsomething.FLVConvertPage;
import jp.sourceforge.akjrcp.dropsomething.H264SettingPage;
import jp.sourceforge.akjrcp.dropsomething.MPEGConvertTabBuilder;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegOptions;
import jp.sourceforge.akjrcp.flvtools.basic.AVISettingPage;
import jp.sourceforge.akjrcp.flvtools.basic.FLVToolsPage;
import jp.sourceforge.akjrcp.flvtools.basic.MPEGSettingPage;
import jp.sourceforge.akjrcp.flvtools.basic.ShareSettingPage;
import jp.sourceforge.akjrcp.flvtools.basic.WMVSettingPage;
import jp.sourceforge.akjrcp.flvtools.dvdstyler.DVDStylerPage;
import jp.sourceforge.akjrcp.flvtools.explorer.ExplorerImageMagickPage;
import jp.sourceforge.akjrcp.flvtools.search.SearchPage;
import jp.sourceforge.akjrcp.greycstoration.GreycstorationPage;
import jp.sourceforge.akjrcp.potrace.PotraceSettingPage;
import jp.sourceforge.akjrcp.psp.PSPHQFieldEditor;
import jp.sourceforge.akjrcp.psp.PSPSettingPage;
import jp.sourceforge.akjrcp.subtitle.SubtitlePage;
import jp.sourceforge.akjrcp.tools.OtherToolsSettingPage;
import jp.sourceforge.akjrcp.util.DefaultSetter;
import jp.sourceforge.akjrcp.util.FileNameUtils;
import jp.sourceforge.akjrcp.videotools.VideoToolsSettingPage;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;

import com.akjava.akjbetas.vocaloid.VoiceSettingPage;
import com.akjava.akjbetas.vocaloid.WavToVsqSettingPage;

public class BetasPreferenceInitializer extends AbstractPreferenceInitializer{

	public void initializeDefaultPreferences() {
		Akj_betasPlugin.log.info("BetasPreferenceInitializer start");
		
	
		
		//akj preference
		IPreferenceStore store =
	           Akj_betasPlugin.getDefault().getPreferenceStore();
		//explorer
		store.setDefault(FLVToolsPage.EXPORT_SAME_DIRECTORY,true);
		
		//wmv settings.
		store.setDefault(WMVSettingPage.WMV_VIDEO_CODEC, FFMpegOptions.CODEC_VIDEO_WMV2);//guess stable this.
		store.setDefault(WMVSettingPage.WMV_AUDIO_CODEC, FFMpegOptions.CODEC_AUDIO_WMA1);
	
		//avi setting.
		store.setDefault(AVISettingPage.AVI_VIDEO_CODEC, FFMpegOptions.CODEC_VIDEO_MSMPEG4V2);
		store.setDefault(AVISettingPage.AVI_AUDIO_CODEC, FFMpegOptions.CODEC_AUDIO_WMA2);
		
		
		//mpeg setting.
		store.setDefault(MPEGSettingPage.MPEG_VIDEO_CODEC, FFMpegOptions.CODEC_VIDEO_MPEG1);
		store.setDefault(MPEGSettingPage.MPEG_AUDIO_CODEC, FFMpegOptions.CODEC_AUDIO_MP2);
		store.setDefault(MPEGSettingPage.MPEG_TYPE,MPEGSettingPage.MPEG_TYPE_VBR);
	
		
		//mpegconvert setting.
		store.setDefault(MPEGConvertTabBuilder.MPEGCONVERT+"."+MPEGConvertTabBuilder.AVISYNTH,false);
		store.setDefault(MPEGConvertTabBuilder.MPEGCONVERT+"."+MPEGConvertTabBuilder.VIDEO_BITRATE,50);
		
		
		
		//video tools
//		tmpdir
		String tmpPath=System.getProperty("java.io.tmpdir");
		if(!FileNameUtils.isAsciiFileName(tmpPath)){
			tmpPath="c:\\tmp\\";
		}
		store.setDefault(OtherToolsSettingPage.PATH_ASCII_TEMPORARY, tmpPath);
		//mplex
		DefaultSetter.setFilePath(store,VideoToolsSettingPage.PATH_MPLEX,DefaultSetter.toInstalledDirectory(new String[]{
		"dvdauthor\\mplex.exe"},new String[]{}));
		
		//spumux
		DefaultSetter.setFilePath(store,VideoToolsSettingPage.PATH_SPUMUX,DefaultSetter.toInstalledDirectory(new String[]{
				"dvdauthor\\spumux.exe"},new String[]{}));
		//atomicparsley
		DefaultSetter.setFilePath(store,VideoToolsSettingPage.PATH_ATOMIC_PARSLEY,DefaultSetter.toInstalledDirectory(new String[]{
		"AtomicParsley\\AtomicParsley.exe","AtomicParsley.exe"},new String[]{}));
		
		//asfbin
		DefaultSetter.setFilePath(store,VideoToolsSettingPage.PATH_ASFBIN,DefaultSetter.toInstalledDirectory(new String[]{
		"asfbin\\asfbin.exe","asfbin.exe"},new String[]{}));
		
		//mplay 
		DefaultSetter.setFilePath(store,VideoToolsSettingPage.PATH_MPLAYER,DefaultSetter.toInstalledDirectory(new String[]{"mplayer\\mplayer.exe"},new String[]{
				
				"c:\\Program Files\\MediaCoder\\codecs\\mplayer.exe",
				"c:\\Program Files\\MediaCoder\\mplayer\\mplayer.exe",
		})
		);
		
		//audio tools
		DefaultSetter.setFilePath(store,AudioToolsSettingPage.PATH_PITCH2,DefaultSetter.toInstalledDirectory(new String[]{
		"akjsnack\\pitch2.exe"},new String[]{}));
		DefaultSetter.setFilePath(store,AudioToolsSettingPage.PATH_SPECTRAM,DefaultSetter.toInstalledDirectory(new String[]{
		"akjsnack\\spectram.exe"},new String[]{}));
		DefaultSetter.setFilePath(store,AudioToolsSettingPage.PATH_FORMANT,DefaultSetter.toInstalledDirectory(new String[]{
		"akjsnack\\formant.exe"},new String[]{}));
		
		File spectramDir=new File(System.getProperty("user.dir")+"\\"+"temporary"+"\\"+"spectram");
		spectramDir.mkdirs();
		store.setDefault(AudioToolsSettingPage.PATH_SPECTRAM_IMAGES, spectramDir.getAbsolutePath());
		
		//psp
		store.setDefault(PSPSettingPage.HQ_SETTINGS, PSPHQFieldEditor.HQSettings[0]);
		store.setDefault(PSPSettingPage.DROP_FILTER_VIDEO, "flv wmv mp4 avi mpeg mpg ts vob mkv rmvb");
		store.setDefault(PSPSettingPage.MAKE_THUMBNAIL, true);
		
		
		try {
			
			String text=IOUtils.toString(this.getClass().getResourceAsStream("/jp/sourceforge/akjrcp/potrace/resource/potrace_settings.csv"),"SJIS");
			
			store.setDefault(PotraceSettingPage.EXPORT_POTRACE_SETTINGS, text);
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		
		try {
			String text=IOUtils.toString(this.getClass().getResourceAsStream("/jp/sourceforge/akjrcp/dropsomething/resource/ffmpeg_settings.csv"),"SJIS");
			
			store.setDefault(FFMpegSettingPage.EXPORT_FFMPEG_SETTINGS, text);
			
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		
		
		//h264s
		try {
			String text=IOUtils.toString(this.getClass().getResourceAsStream("/jp/sourceforge/akjrcp/dropsomething/resource/h264settings.txt"),"SJIS");
			
			store.setDefault(H264SettingPage.EXPORT_H264_SETTINGS, text);
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		
		
		
		String os=System.getProperty("os.name");
		//log.info(os);
		
		
		
		
		
		try{
		store.setDefault(ShareSettingPage.VIDEO_FPS,""); //$NON-NLS-1$
		store.setDefault(ShareSettingPage.VIDEO_BITRATE,"756"); //$NON-NLS-1$
		store.setDefault(ShareSettingPage.VIDEO_SIZE, "");
		store.setDefault(ShareSettingPage.VIDEO_SIZE_OTHER,FFMpegOptions.SIZE_160x120);
		store.setDefault(ShareSettingPage.VIDEO_FPS_OTHER,FFMpegOptions.VIDEO_RATE_1);
		
		//store.setDefault(ShareSettingPage.VIDEO_CODEC,"msmpeg4v2");
		store.setDefault(ShareSettingPage.IMAGE_TIME,"00:00:05");
		store.setDefault(ShareSettingPage.EXPORT_WAVE_CHANNEL,"2"); //$NON-NLS-1$
		store.setDefault(ShareSettingPage.EXPORT_WAVE_SAMPLERATE,ShareSettingPage.RATE_48000);
		store.setDefault(ShareSettingPage.AUDIO_BITRATE,"96"); //$NON-NLS-1$
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(os.toLowerCase().indexOf("vista")!=-1){
		//for Vista	
		//Desktop and Mydocument can't write
		DefaultSetter.setFilePaths(store,FLVToolsPage.EXPLORER_PATHS,new String[]{System.getProperty("user.home")+"\\Documents\\flv_files",System.getProperty("user.home")+"\\desktop\\flv_files",System.getProperty("user.home")+"\\videos\\flv_files",System.getProperty("user.home")+"\\Videos\\",System.getProperty("user.home")+"\\Documents\\",System.getProperty("user.home")+"\\Desktop\\",System.getProperty("user.dir")+"\\flv_files"});
			
		}else{
		//for XP
		DefaultSetter.setFilePaths(store,FLVToolsPage.EXPLORER_PATHS,new String[]{System.getProperty("user.home")+"\\My Documents\\flv_files",System.getProperty("user.home")+"\\�f�X�N�g�b�v\\flv_files",System.getProperty("user.home")+"\\desktop\\flv_files",System.getProperty("user.home")+"\\My Documents\\My Videos",System.getProperty("user.home")+"\\My Documents",System.getProperty("user.home")+"\\�f�X�N�g�b�v",System.getProperty("user.dir")+"\\flv_files"});
		}
		DefaultSetter.setFilePath(store,FLVToolsPage.PLAYER_PATH,DefaultSetter.toInstalledDirectory(new String[]{"akj_friends_2006_summer\\VLC\\vlc.exe","VideoLAN\\VLC\\vlc.exe","Media Player Classic\\mplayerc.exe","mpc2kxp6490\\mplayerc.exe"},new String[]{}));
		DefaultSetter.setFilePath(store,FLVToolsPage.FLVTOOL2_PATH,DefaultSetter.toInstalledDirectory(new String[]{"akj_friends_2006_summer\\flvtool2\\flvtool2.exe"},new String[]{}));
		
		store.setDefault(SearchPage.USE_FIREFOX,true);
		store.setDefault(SearchPage.MODE_SORT,SearchPage.SORT_DATE);
		DefaultSetter.setFilePath(store,DVDStylerPage.DVDSTYLER_PATH,DefaultSetter.toInstalledDirectory(new String[]{"DVDStyler\\bin\\DVDStyler.exe"},new String[]{"c:\\Program Files\\DVDStyler\\bin\\DVDStyler.exe"}));
	
		store.setDefault(FLVToolsPage.SUPPORTED_EXTENSIONS, "mp4");
	
		store.setDefault(FLVToolsPage.PRIMARY_FLOWPLAYER,true);
	//mkdirs
		File f=new File(System.getProperty("user.dir")+"\\"+"thumbs");
		if(!f.exists()){
			f.mkdir();
		}
		
		File f2=new File(System.getProperty("user.dir")+"\\"+"FLV_files");
		if(!f2.exists()){
			f2.mkdir();
		}
		//flowPlayerDir=new File(toPluginDir(this),"flowplayer");
		//File flowPlayerDir=new File(System.getProperty("user.dir")+"\\"+"flowplayer");
		
		store.setDefault(FLVToolsPage.FLV_EXTENSION_ADD,FLVToolsPage.FLV_EXTENSION_ADD_AUTO);
		
	//flv convert
		store.setDefault(FLVConvertPage.FLVCONVERT_VIDEO_SIZE,"320x240");
		store.setDefault(FLVConvertPage.FLVCONVERT_VIDEO_RATE,""+30);
		store.setDefault(FLVConvertPage.FLVCONVERT_VIDEO_BITRATE,""+1500);
		
		store.setDefault(FLVConvertPage.FLVCONVERT_AUDIO_RATE,PreferenceUtils.RATE_44100);
		store.setDefault(FLVConvertPage.FLVCONVERT_AUDIO_CHANNEL,1);
		store.setDefault(FLVConvertPage.FLVCONVERT_AUDIO_BITRATE,""+128);
		
		store.setDefault(FLVConvertPage.FLVCONVERT_USE_AVISYNTH,false);//
		store.setDefault(FLVConvertPage.FLVCONVERT_USE_FLVTOOL2,false);//maybe problem
		
		//Greycstoration
		DefaultSetter.setFilePath(store,GreycstorationPage.GREYCSTORATION_PATH,DefaultSetter.toInstalledDirectory(new String[]{
				"GREYCstoration-2.5.2\\GREYCstoration_pc_win32.exe",
				"GREYCstoration-2.5.1\\GREYCstoration_pc_win32.exe"
				,"GREYCstoration-2.5\\GREYCstoration_win32.exe"
		},new String[]{}));
		
		
		//potrace
		DefaultSetter.setFilePath(store,PotraceSettingPage.POTRACE_PAHT,DefaultSetter.toInstalledDirectory(new String[]{
				"potrace\\potrace.exe"
		},new String[]{}));
	
		DefaultSetter.setFilePath(store,PotraceSettingPage.MKBITMAP_PAHT,DefaultSetter.toInstalledDirectory(new String[]{
				"potrace\\mkbitmap.exe"
		},new String[]{}));

		//Imagemagick
		File programFiles=new File("c:\\Program Files");
		String[] list=programFiles.list();
		if(list!=null){
		ArrayUtils.reverse(list);
		for (int i = 0; i < list.length; i++) {
			if(list[i].startsWith("ImageMagick")){
				File convert=new File(programFiles,list[i]+"\\convert.exe");
				store.setDefault(OtherToolsSettingPage.PATH_IMAGEMAGICK_CONVERT, convert.getAbsolutePath());
				
				File montage=new File(programFiles,list[i]+"\\montage.exe");
				store.setDefault(OtherToolsSettingPage.PATH_IMAGEMAGICK_MONTAGE, montage.getAbsolutePath());
			}
		}
		}
		
		store.setDefault(ExplorerImageMagickPage.GIF_ANIME_SIZE, "320x240");
		store.setDefault(ExplorerImageMagickPage.GIF_ANIME_WAIT, 50);
		store.setDefault(ExplorerImageMagickPage.GIF_ANIME_LOOP, 0);
		store.setDefault(ExplorerImageMagickPage.IMAGE_LIST_SIZE, "160x120");
		store.setDefault(ExplorerImageMagickPage.IMAGE_LIST_W, "3");
		store.setDefault(ExplorerImageMagickPage.IMAGE_LIST_H, "4");
		
		//wavtovsq
		try{
			String text=IOUtils.toString(this.getClass().getResourceAsStream("/com/akjava/vocaloidtools/wavtovsq/resources/default_voice.csv"),"SJIS");
			
		store.setDefault(VoiceSettingPage.EXPORT_VOICE_SETTINGS,text);
		text=IOUtils.toString(this.getClass().getResourceAsStream("/com/akjava/vocaloidtools/wavtovsq/resources/default_wav2vsq.csv"),"SJIS");
		
		store.setDefault(WavToVsqSettingPage.EXPORT_WAVTOVSQ_SETTINGS,text);
		}catch(Exception e){
e.printStackTrace();
		}
		//subtitle
		store.setDefault(SubtitlePage.DEFAULT_ENCODING, SubtitlePage.ENCODING_UTF_8);
		
		//custom command
		store.setDefault(FFMpegSettingPage.DROPFFMPEG_CUSTOM_COMMAND, "explorer \"${OUTPUT}\"");
		Akj_betasPlugin.log.info("BetasPreferenceInitializer end");
	}
	


}
